package com.ansion.commonchat.startup.workmanager.bwork;

import android.annotation.SuppressLint;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;

import com.google.common.util.concurrent.ListenableFuture;

/**
 * Created by wanghongjie on 2023/5/11
 * Describe : 用于表示对后台工作的操作，例如取消工作、观察工作状态、添加监听器等。
 * 通过 Operation 接口，可以执行各种操作以管理和监控后台工作的执行
 */
public interface BOperation {

    @SuppressWarnings("VariableNameSameAsType")
    @SuppressLint("SyntheticAccessor")
    State.SUCCESS SUCCESS = new State.SUCCESS();

    @SuppressWarnings("VariableNameSameAsType")
    @SuppressLint("SyntheticAccessor")
    State.IN_PROGRESS IN_PROGRESS = new State.IN_PROGRESS();

    @NonNull
    LiveData<State> getState();

    @NonNull
    ListenableFuture<State.SUCCESS> getResult();

    abstract class State {

        State() {
        }

        public static final class SUCCESS extends State {
            private SUCCESS() {
                super();
            }

            @Override
            @NonNull
            public String toString() {
                return "SUCCESS";
            }
        }

        public static final class IN_PROGRESS extends State {
            private IN_PROGRESS() {
                super();
            }

            @Override
            @NonNull
            public String toString() {
                return "IN_PROGRESS";
            }
        }

        public static final class FAILURE extends State {

            private final Throwable mThrowable;

            public FAILURE(@NonNull Throwable exception) {
                super();
                mThrowable = exception;
            }

            @NonNull
            public Throwable getThrowable() {
                return mThrowable;
            }

            @Override
            @NonNull
            public String toString() {
                return String.format("FAILURE (%s)", mThrowable.getMessage());
            }
        }
    }
}